﻿using System;
using System.Data;
using System.Data.Common;

namespace BalloonShop.App_Code
{
    /// <summary>
    /// 封装门类详细数据
    /// </summary>
    public struct DepartmentDetails {
        public string Name;
        public string Description;
    }

    /// <summary>
    /// 封装商品详细信息
    /// </summary>
    public struct ProductDetails {
        public int ProductID;
        public string Name;
        public string Description;
        public decimal Price;
        public string Thumbnail;
        public string Image;
        public bool PromoFront;
        public bool PromoDept;
    }

    /// <summary>
    /// 商品目录的业务层组件
    /// </summary>
    public static class CatalogAccess
    {
        static CatalogAccess()
        {
            //
            //TODO:在此添加构造函数逻辑
            //
        }

        //检索门类列表
        public static DataTable GetDepartments()
        {
            //获取一个已配置的DbCommand对象
            DbCommand comm = GenericDataAccess.CreateCommand();
            //设置存储过程名称
            comm.CommandText = "GetDepartments";
            //执行该存储过程并返回结果
            return GenericDataAccess.ExcuteSelectCommand(comm);
        }

        //获取门类详细信息
        public static DepartmentDetails GetDepartmentDetails(string departmentID) {
            //获取已配置的DbCommand对象
            DbCommand comm = GenericDataAccess.CreateCommand();
            //设置存储过程名称
            comm.CommandText = "CatalogGetDepartmentDetails";
            //创建新的参数
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@DepartmentID";
            param.Value = departmentID;
            comm.Parameters.Add(param);
            //执行该存储过程
            DataTable table = GenericDataAccess.ExcuteSelectCommand(comm);
            //将获取的信息封装到DepartmentDetails对象
            DepartmentDetails details = new DepartmentDetails();
            if (table.Rows.Count > 0)
            {
                details.Name = table.Rows[0]["Name"].ToString();
                details.Description = table.Rows[0]["Description"].ToString();
            }
            //返回门类详细信息
            return details;
        }

        //获取商品详细信息
        public static ProductDetails GetProductDetails(string productId) {
            //获取一个已配置的DbCommand对象
            DbCommand comm = GenericDataAccess.CreateCommand();
            //设置存储过程的名称
            comm.CommandText = "CatalogGetProductDetails";
            //创建一个新的参数
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@ProductID";
            param.Value = productId;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            //执行这个存储过程
            DataTable table = GenericDataAccess.ExcuteSelectCommand(comm);
            //将获取的数据封装到ProductDetails对象中
            ProductDetails details = new ProductDetails();
            if (table.Rows.Count > 0)
            {
                //获取第一个表行
                DataRow dr = table.Rows[0];
                //获取商品详细信息
                details.ProductID = int.Parse(productId);
                details.Name = dr["Name"].ToString();
                details.Description = dr["Description"].ToString();
                details.Price = Decimal.Parse(dr["Price"].ToString());
                details.Thumbnail = dr["Thumbnail"].ToString();
                details.Image = dr["Image"].ToString();
                details.PromoFront = bool.Parse(dr["PromoFront"].ToString());
                details.PromoDept = bool.Parse(dr["PromoDept"].ToString());
            }
            //返回门类详细信息
            return details;
        }

        //获取某门类下的分类列表
        public static DataTable GetCatagoriesInDepartment(string departmentId)
        {
            //获取已配置的DbCommand对象
            DbCommand comm = GenericDataAccess.CreateCommand();
            //设置存储过程名称
            comm.CommandText = "CatalogGetCategoriesInDepartment";
            //创建一个新的参数
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@DepartmentID";
            param.Value = departmentId;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            //执行该存储过程
            return GenericDataAccess.ExcuteSelectCommand(comm);
        }

        //获取属于推荐分类的商品列表
        public static DataTable GetProductsOnFrontPromo(string pageNumber, out int howManyPages)
        {
            //获取一个已配置的DbCommand对象
            DbCommand comm = GenericDataAccess.CreateCommand();
            //设置存储过程名称
            comm.CommandText = "CatalogGetProductsOnFrontPromo";
            //创建一个新的参数
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@DescriptionLength";
            param.Value = BalloonShopConfiguration.ProductDescriptionLength;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            //创建一个新的参数
            param = comm.CreateParameter();
            param.ParameterName = "@ProductsPerPage";
            param.Value = BalloonShopConfiguration.ProductsPerPage;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            //创建一个新的参数
            param = comm.CreateParameter();
            param.ParameterName = "@HowManyProducts";
            param.Direction = ParameterDirection.Output;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            //执行该存储过程，并将结果保存在一个DataTable中
            DataTable table = GenericDataAccess.ExcuteSelectCommand(comm);
            //计算商品的页数，并设置out参数
            int howManyProducts = Int32.Parse(comm.Parameters["@HowManyProducts"].Value.ToString());
            howManyPages = (int)Math.Ceiling((double)howManyProducts / (double)BalloonShopConfiguration.ProductsPerPage);
            //返回商品页面
            return table;
        }

        //获取指定分类的商品列表
        public static DataTable GetProductsOnDeptPromo(string departmentId, string pageNumber, out int howManyPages)
        {
            // get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            comm.CommandText = "CatalogGetProductsOnDeptPromo";
            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@DepartmentID";
            param.Value = departmentId;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@DescriptionLength";
            param.Value = BalloonShopConfiguration.ProductDescriptionLength;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@PageNumber";
            param.Value = pageNumber;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@ProductsPerPage";
            param.Value = BalloonShopConfiguration.ProductsPerPage;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@HowManyProducts";
            param.Direction = ParameterDirection.Output;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // execute the stored procedure and save the results in a DataTable
            DataTable table = GenericDataAccess.ExcuteSelectCommand(comm);
            // calculate how many pages of products and set the out parameter
            int howManyProducts = Int32.Parse(comm.Parameters["@HowManyProducts"].Value.ToString());
            howManyPages = (int)Math.Ceiling((double)howManyProducts /
                           (double)BalloonShopConfiguration.ProductsPerPage);
            // return the page of products
            return table;
        }

        //获取指定门类的特色商品列表
        public static DataTable GetProductsInCategory
        (string categoryId, string pageNumber, out int howManyPages)
        {
            // get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            comm.CommandText = "CatalogGetProductsInCategory";
            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@CategoryID";
            param.Value = categoryId;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@DescriptionLength";
            param.Value = BalloonShopConfiguration.ProductDescriptionLength;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@PageNumber";

            param.Value = pageNumber;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@ProductsPerPage";
            param.Value = BalloonShopConfiguration.ProductsPerPage;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@HowManyProducts";
            param.Direction = ParameterDirection.Output;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // execute the stored procedure and save the results in a DataTable
            DataTable table = GenericDataAccess.ExcuteSelectCommand(comm);
            // calculate how many pages of products and set the out parameter
            int howManyProducts = Int32.Parse(comm.Parameters["@HowManyProducts"].Value.ToString());
            howManyPages = (int)Math.Ceiling((double)howManyProducts /
                           (double)BalloonShopConfiguration.ProductsPerPage);
            // return the page of products
            return table;
        }
    }
}